import { isInArray } from "@/utils"

/* 负责建筑相关任务 */
export default class StructureMissonExtension extends Room {
    public Task_SlinkToClink():void{
        if ((global.Gtime[this.name]- Game.time) % 13) return
        if (!this.memory.Misson['Structure']) this.memory.Misson['Structure'] = []
        if (!this.memory.StructureIdData.source_link || !this.memory.StructureIdData.center_link) return
        var source_link = Game.getObjectById(this.memory.StructureIdData.source_link as string) as StructureLink
        var center_link =  Game.getObjectById(this.memory.StructureIdData.center_link as string) as StructureLink
        if (!source_link)
        {
            delete this.memory.StructureIdData.source_link
            return
        }
        if (!center_link)
        {
            delete this.memory.StructureIdData.center_link
            return
        }
        if (source_link.store.getUsedCapacity('energy') >= 600)
        {
            var thisTask = this.Public_Link([source_link.id],center_link.id,10)
            this.AddMisson(thisTask)
        }
        if (this.MissonNum('Structure','链传送能') >= 1) return
        else
        {
            if (!this.memory.StructureIdData.source2_link) return
            var source2_link = Game.getObjectById(this.memory.StructureIdData.source2_link as string) as StructureLink
            if (!source2_link)
            {
                delete this.memory.StructureIdData.source2_link
                return
            }
            if (source2_link.store.getUsedCapacity('energy') >= 400)
            {
                var thisTask = this.Public_Link([source2_link.id],center_link.id,10)
                this.AddMisson(thisTask)
            }
        }
    }


    /* 房间内防御塔工作 */

    public TowerWork():void{
        if (!global.TowerData) global.TowerData = {}
        if (!global.TowerData[this.name]) global.TowerData[this.name] = {}
        var RoomTowerData = global.TowerData[this.name]
        if (RoomTowerData.towerOccupy)
        {
            RoomTowerData.towerObjectList = []
            for (var ot of global.Stru[this.name]['Otower'] as StructureTower[]) RoomTowerData.towerObjectList.push(ot)
            RoomTowerData.towerObjectList.push(global.Stru[this.name]['Ntower'] as StructureTower)
            if(!Game.creeps[RoomTowerData.towerOccupy])
            {
                delete RoomTowerData.towerOccupy
                delete RoomTowerData.towerObjectList
                return
            }
            return
        }
        if (this.memory.roomSTATE == 'peace')
        {
            var thisTower = global.Stru[this.name]['Ntower'] as StructureTower
            if (!thisTower) return
            if (Game.flags[`${this.name}/repair`])
            {
                var towers = this.find(FIND_MY_STRUCTURES,{filter:(stru)=>{
                    return stru.structureType == 'tower' && stru.id != this.memory.StructureIdData.NtowerID
                }}) as StructureTower[]
                var ramparts = this.getListHitsleast(['rampart','constructedWall'])
                if ((Game.time - global.Gtime[this.name])% 5 == 0)
                {
                    var repairRoad = thisTower.pos.findClosestByRange(FIND_STRUCTURES,{filter:(stru)=>{
                        return (stru.structureType == 'road' || stru.structureType == 'container') && stru.hits/stru.hitsMax < 0.8
                    }})
                    if (repairRoad) thisTower.repair(repairRoad)
                }
                else
                {
                    thisTower.repair(ramparts)
                    for (var t of towers) t.repair(ramparts)
                }
                return
            }
            if ((Game.time - global.Gtime[this.name] )% 5 == 0)
            {
                var repairRoad = thisTower.pos.findClosestByRange(FIND_STRUCTURES,{filter:(stru)=>{
                    return (stru.structureType == 'road' || stru.structureType == 'container') && stru.hits/stru.hitsMax < 0.8
                }})
                if (repairRoad) thisTower.repair(repairRoad)
            }
        }
        else
        {
            if (Game.flags[`${this.name}/stop`]) return
            /* 没有防御塔支配下的防御 */
            var towers = this.find(FIND_MY_STRUCTURES,{filter:(stru)=>{
                return stru.structureType == 'tower'
            }}) as StructureTower[]
            if (this.MissonNum('Creep','主动防御') > 0)
            {
                /* 出了主动防御就先修修墙，因为攻击都是徒劳的 */
                var ramparts = this.getListHitsleast(['rampart','constructedWall'])
                if (Game.time % 3 == 0)
                {
                    for (var t of towers)
                    {
                        t.repair(ramparts)
                    }
                }
                return
            }
            if (!RoomTowerData.enemyindex) RoomTowerData.enemyindex = 0
            var enemy = this.find(FIND_HOSTILE_CREEPS,{filter:(creep)=>{
                return !isInArray(Memory.whitesheets,creep.owner.username)
            }})
            if (enemy.length > 1)
            for (var t of towers)
            {
                t.attack(enemy[RoomTowerData.enemyindex])
            }
            else if (enemy.length == 1)
            {
                for (var t of towers)
                {
                    t.attack(enemy[0])
                }
            }
            if (RoomTowerData.enemyindex >= 1)
            {
                RoomTowerData.enemyindex = 0
                return
            }
            RoomTowerData.enemyindex += 1
        }

    }

    
}